package com.qianfeng.openplatform.geteway.fiter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.commons.field.ResponseMessage;
import com.qianfeng.openplatform.commons.field.StaticField;
import com.qianfeng.openplatform.geteway.client.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 江江
 * @Date 2020/7/28 11:46 http://manager.openapi.sys/  或者localhost
 * 应用管理===>日调用量限制
 * 如:开启免费 100次
 * http://www.openapi.sys/openapi?method=servertest04&fwcsgl=lisi&name=lisi&appkey=msmsms23330
 */
@Component
public class LimitFllter extends ZuulFilter {
    @Autowired
    private CacheClient cacheClient;
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER+10;
    }

    @Override
    public boolean shouldFilter() {
        System.out.println("LimitFllter.shouldFilter ===============>38行");
        boolean isFee =false;
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String method = request.getParameter("method");
        String needfee = null;
        try {
            needfee = cacheClient.hGet(RedisKeyPrefix.WEB_MASTER_API_MAPPING_HASH_PRE + method, "needfee");
            isFee="0".equals(needfee)?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return currentContext.sendZuulResponse()&&isFee;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("LimitFllter.run ===============>55行");
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String appkey = request.getParameter("appkey");
        try {
            Long lint = cacheClient.hIncrement(RedisKeyPrefix.WEB_MASTER_API_SERVICE_APPKEY_KEY + appkey, "linit", -1l);
            if (lint<=0){
                currentContext.setSendZuulResponse(false);
                ObjectMapper objectMapper = new ObjectMapper();
                HttpServletResponse response = currentContext.getResponse();
                response.setContentType("application/json;charset=utf-8");
                response.getWriter().write(objectMapper.writeValueAsString(new ResponseMessage(StaticField.REQEST_PARAM_ERROR_CODE,"访问超过本日限制....")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}





